package com.example.controller;

import com.example.common.Result;
import com.example.pojo.Account;
import com.example.pojo.Admin;
import com.example.pojo.LoginResponse;
import com.example.pojo.Owner;
import com.example.service.OwnerService;
import com.example.util.ThreadLocalUtil;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/owner")
public class OwnerController {
    @Resource
    private OwnerService ownerService;

    /**
     * 登录
     * @param owner
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Owner owner) {
       LoginResponse loginResponse = ownerService.login(owner);
        return Result.success(loginResponse);
    }

    @PostMapping("/register")
    public Result register(@RequestBody Owner owner) {
        ownerService.register(owner);
        return Result.success();
    }

    /**
     * 登录测试，打印当前登录用户
     */
    @GetMapping("/userInfo")
    public Result userInfo() {
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        Owner owner = ownerService.selectByUsername(username);
        return Result.success(owner);
    }

    /**
     * 修改密码
     * @param account
     * @param token
     * @return
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account, @RequestHeader("Authorization") String token) {
        ownerService.updatePassword(account,token);
        return Result.success(account);
    }

    /**
     * 添加用户
     * @param owner
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Owner owner) {
        ownerService.add(owner);
        return Result.success();
    }

    /**
     * 修改用户
     * @param owner
     * @return
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Owner owner) {
        ownerService.updateById(owner);
        return Result.success();
    }

    /**
     * 删除用户
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam Integer id) {
        ownerService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除用户
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        ownerService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(Owner owner) {
        List<Owner> list = ownerService.selectAll(owner);
        return Result.success(list);
    }

    /**@PathVariable的查询方式/selectById/1(/..)
     * 根据id查询用户
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Owner Owner = ownerService.selectById(id);
        return Result.success(Owner);
    }

    /**@RequestParam的查询方式/selectPage?pageNum=1(&...=...)
     * @RequestParam(defaultValue = "1")默认值为1
     *@RequestParam(required = false)不是必须要传参
     * 分页查询
     */
    @GetMapping("/SelectPage")
    public Result selectPage(Owner Owner,@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo<Owner> pageInfo= ownerService.selectPage(Owner,pageNum,pageSize);
        return Result.success(pageInfo);
    }
    /**
        获取房东统计数
     */
    @GetMapping("/statistics")
    public Result getStatistcs(@RequestParam Integer ownerId) {
        Map<String,Object> map = ownerService.statistcs(ownerId);
        return Result.success(map);
    }

}
